package com.vshop.framework.satoken.autoconfig;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.jwt.StpLogicJwtForSimple;

import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpLogic;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.vshop.framework.core.constant.PropertiesConst;
import com.vshop.framework.core.util.StringConst;
import com.vshop.framework.core.util.YmlPropertySourceFactory;
import com.vshop.framework.satoken.autoconfig.properties.SaTokenExtensionProperties;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Slf4j
@RequiredArgsConstructor
@AutoConfiguration
@EnableConfigurationProperties(SaTokenExtensionProperties.class)
@ConditionalOnProperty(prefix = "sa-token.extension", name = PropertiesConst.Enabled, havingValue = "true")
@PropertySource(value = "classpath:default-satoken.yml", factory = YmlPropertySourceFactory.class)
public class SaTokenAutoConfig implements WebMvcConfigurer {

    private final SaTokenExtensionProperties properties;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(SpringUtil.getBean(SaInterceptor.class)).addPathPatterns(StringConst.PATH_PATTERN);
    }

    /**
     * SaToken 拦截器配置
     */
    @Bean
    @ConditionalOnMissingBean
    public SaInterceptor saInterceptor() {
        return new SaInterceptor(handle -> SaRouter.match(StringConst.PATH_PATTERN)
                .notMatch(properties.getSecurity().getExcludes())
                .check(r -> StpUtil.checkLogin()));
    }

    /**
     * 持久层配置
     */
    @Configuration
    @Import({SaTokenDaoConfig.Default.class, SaTokenDaoConfig.Redis.class,
            SaTokenDaoConfig.Custom.class})
    protected static class SaTokenDaoAutoConfig {
    }

    /**
     * 整合 JWT（简单模式）
     */
    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = "sa-token.extension", name = "enableJwt", havingValue = "true")
    public StpLogic stpLogic() {
        return new StpLogicJwtForSimple();
    }


    /**
     * 权限接口实现(使用bean注入方便用户替换)
     */
//    @Bean
//    public StpInterface stpInterface() {
//        return new SaPermissionImpl();
//    }
    @PostConstruct
    public void postConstruct() {
        log.info("初始化 SaTokenAutoConfig");
    }
}
